﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class FAQ2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //The main function for the TreeView
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    PopulateCategory(e.Node);
                    break;
                case 1:
                    PopulateQuestion(e.Node);
                    break;
                case 2:
                    PopulateAnswer(e.Node);
                    break;
            }
        }
    }

    //For populating the root node.
    //Example, Interface, Beginner, Immediate, Expert FAQ
    void PopulateCategory(TreeNode node)
    {
        SqlCommand sqlQuery = new SqlCommand(
            "SELECT DISTINCT Question.CategoryID, Category.CategoryName FROM Category " +
            "INNER JOIN Question ON Category.CategoryID = Question.CategoryID " + 
            "ORDER BY Question.CategoryID");
        DataSet resultSet = RunQuery(sqlQuery);
        if (resultSet.Tables.Count > 0)
        {
            foreach (DataRow row in resultSet.Tables[0].Rows)
            {
                TreeNode NewNode = new TreeNode(row["CategoryName"].ToString());
                NewNode.PopulateOnDemand = true;
                NewNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(NewNode);
            }
        }
    }

    //For populating the 2nd level, Question.
    //This will contain all the FAQ question of the specific root category.
    void PopulateQuestion(TreeNode node)
    {
        SqlCommand sqlQuery = new SqlCommand(
            "SELECT DISTINCT Question.CategoryID, Category.CategoryName, Question.Question FROM Category " +
            "INNER JOIN Question ON Category.CategoryID = Question.CategoryID " +
            "WHERE CategoryName = @categoryname " +
            "ORDER BY Question.CategoryID");
        sqlQuery.Parameters.Add("@categoryname", SqlDbType.VarChar).Value = node.Value;
        DataSet resultSet = RunQuery(sqlQuery);
        if (resultSet.Tables.Count > 0)
        {
            foreach (DataRow row in resultSet.Tables[0].Rows)
            {
                TreeNode NewNode = new TreeNode(row["Question"].ToString());
                NewNode.PopulateOnDemand = true;
                NewNode.SelectAction = TreeNodeSelectAction.None;
                node.ChildNodes.Add(NewNode);
            }
        }
    }

    //For populating the 3rd level, Answers.
    //This will be the Answer for the specific Question node.
    void PopulateAnswer(TreeNode node)
    {
        SqlCommand sqlQuery = new SqlCommand(
            "SELECT Answer FROM Question " +
            "WHERE Question = @question ");
        sqlQuery.Parameters.Add("@question", SqlDbType.VarChar).Value = node.Value;
        DataSet resultSet = RunQuery(sqlQuery);
        if (resultSet.Tables.Count > 0)
        {
            foreach (DataRow row in resultSet.Tables[0].Rows)
            {
                TreeNode NewNode = new TreeNode(row["Answer"].ToString());
                NewNode.PopulateOnDemand = false;
                NewNode.SelectAction = TreeNodeSelectAction.None;
                node.ChildNodes.Add(NewNode);
            }
        }
    }

    //This will be the connection establishment between SQL and ASP.
    private DataSet RunQuery(SqlCommand sqlQuery)
    {
        string connectionString =
            ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;

        SqlConnection DBConnection;
        DataSet resultsDataSet;
        using (DBConnection = new SqlConnection(connectionString))
        {
            SqlDataAdapter dbAdapter = new SqlDataAdapter();
            dbAdapter.SelectCommand = sqlQuery;
            sqlQuery.Connection = DBConnection;
            resultsDataSet = new DataSet();
            try
            {
                dbAdapter.Fill(resultsDataSet);
            }
            catch
            {
                labelStatus.Text = "Unable to connect to SQL Server.";

            }
            finally
            {
                DBConnection.Close();
            }
        }
        return resultsDataSet;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TreeView treeView = TreeView1;
        treeView.ExpandAll();

        string searchString = txtSearchString.Text.ToLower();
        string currQuestionNodeValue = string.Empty;
        string currAnswerNodeValue = string.Empty;
        string currRootNodeValue = string.Empty;

        TreeNode rNode = treeView.Nodes[0];
        TreeNode qNode = treeView.Nodes[0].ChildNodes[0]; //question leaves

        /*  DONE:
         * 1) We only assume there is 1 question to 1 answer relationship.
         *    if there are multiple relationships, nested loop is required.
         * 2) Search targets are questions and answers. If search string is found
         *    in either question or answer nodes, the question node will be expanded.
         * 3) The search is not case sensitive.
         * 
         * TODO : when the nodes expands, the white rectangular box did not expand!!
         **/

        for (int r = 0; r < rNode.ChildNodes.Count; r++)
        {
            qNode = treeView.Nodes[0].ChildNodes[r];

            for (int i = 0; i < qNode.ChildNodes.Count; i++) //loop at question leaves. 
            {
                currQuestionNodeValue = qNode.ChildNodes[i].Value.ToLower();
                currAnswerNodeValue = qNode.ChildNodes[i].ChildNodes[0].Value.ToLower();

                if (currQuestionNodeValue.Contains(searchString) || currAnswerNodeValue.Contains(searchString))
                    qNode.ChildNodes[i].Expand();
                else
                    qNode.ChildNodes[i].Collapse();
            }
        }
    }
}